home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Apple Game Sprockets / InputSprocket / Sample Drivers / Common Driver Code / ResourceUtils.cp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-17  |  7.3 KB  |  291 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2.  
  3. File:      ResourceUtils.cp
  4.  
  5. Copyright © 1996, 1997, 1998 Apple Computer, Inc., All Rights Reserved
  6.  
  7.  
  8. You may incorporate this sample code into your applications without
  9. restriction, though the sample code has been provided "AS IS" and the
  10. responsibility for its operation is 100% yours.  However, what you are
  11. not permitted to do is to redistribute the source as "DSC Sample Code"
  12. after having made changes. If you're going to re-distribute the source,
  13. we require that you make it clear in the source that the code was
  14. descended from Apple Sample Code, but that you've made changes.
  15.  
  16. *************************************************************************************/
  17.  
  18. #include "String_Utils.h"
  19. #include "ResourceUtils.h"
  20. #include "Common.h" 
  21.  
  22. #ifndef __MACTYPES__
  23. #include <MacTypes.h>
  24. #endif
  25.  
  26. #ifndef __TEXTUTILS__
  27. #include <TextUtils.h>
  28. #endif
  29.  
  30. #ifndef __ERRORS__
  31. #include <Errors.h>
  32. #endif
  33.  
  34. #ifndef __ICONS__
  35. #include <Icons.h>
  36. #endif
  37.  
  38.  
  39. void Resource_GetIndString(short inResourceId, UInt32 inIndex, SInt16 inDestSize, StringPtr outString, OSStatus *error)
  40. {
  41.     WARNING(outString != nil,        "Resource_GetIndString outString was nil");
  42.     WARNING(error != nil,            "Resource_GetIndString error was nil");
  43.     WARNING(inDestSize > 0,            "Resource_GetIndString inDestSize was <= 0");
  44.     WARNING(inIndex <= 0x7fff,        "Resource_GetIndString inIndex to large");
  45.     WARNING(inIndex >= 0x0001,        "Resource_GetIndString inIndex to small");
  46.     
  47.     *error = noErr;                        // assume no error
  48.     
  49.     Str255 loadedString;                // the string we loaded from a resource
  50.     
  51.     // load the string
  52.     GetIndString(loadedString, inResourceId, inIndex);    
  53.     WARNING(loadedString[0] != 0,    "Resource_GetIndString failed to get a string");
  54.  
  55.     // check and return an error
  56.     if (loadedString[0] == 0) 
  57.     {
  58.         outString[0] = 0;
  59.         *error = memFullErr;
  60.         return;
  61.     }
  62.     
  63.     // copy the string
  64.     CopyPStr(loadedString, outString, inDestSize);
  65. }
  66.  
  67.  
  68. Handle Resource_Get1IconSuite(short inResourceID, IconSelectorValue inSelector, OSStatus *error)
  69. {
  70.     const UInt32    kNumIconTypes = 9;
  71.     const OSType    iconResourceTypes[kNumIconTypes] = 
  72.                                    {    'ICN#', 'icl4', 'icl8',
  73.                                     'ics#',    'ics4',    'ics8',
  74.                                     'icm#',    'icm4',    'icm8' };    // these res types also enumerated in ISpUserInterface.cp
  75.     const IconSelectorValue iconSelectors[kNumIconTypes] = 
  76.                                   {    svLarge1Bit,    svLarge4Bit,    svLarge8Bit,
  77.                                     svSmall1Bit,    svSmall4Bit,    svSmall8Bit,
  78.                                     svMini1Bit,        svMini4Bit,        svMini8Bit };
  79.                                     
  80.     Handle iconHandles[kNumIconTypes] = {    nil, nil, nil,
  81.                                             nil, nil, nil,
  82.                                             nil, nil, nil };
  83.  
  84.     Handle theIconSuite = nil;            // the icon suite we are trying to create
  85.     UInt32 iconTypeItr;                    // use to loop through the different members of the suite
  86.     OSType type;                        // type for this iteration
  87.     Handle handle;                        // handle for this iteration
  88.     Boolean found = false;                // true if we have found any icons to add to the suite
  89.     
  90.     // get and clean the resources
  91.     for(iconTypeItr = 0; iconTypeItr < kNumIconTypes; iconTypeItr++)
  92.     {
  93.         type = iconResourceTypes[iconTypeItr];
  94.         handle = Get1Resource(type, inResourceID);
  95.  
  96.         // if we don't want this selector skip it
  97.         if (!(iconSelectors[iconTypeItr] & inSelector)) { continue; }
  98.     
  99.         if (handle)
  100.         {
  101.             Resource_CleanResHandle(handle);
  102.             iconHandles[iconTypeItr] = handle;
  103.             
  104.             found = true;
  105.         }
  106.     }    
  107.  
  108.     // == found nothing return nil & resNotFound ==
  109.     if (!found)
  110.     {
  111.         *error = resNotFound;
  112.         return nil;
  113.     }    
  114.  
  115.     // == create a new icon suite ==
  116.     *error = NewIconSuite(&theIconSuite);
  117.     
  118.     // == handle that error if required ==
  119.     if (*error)
  120.     {
  121.         for(iconTypeItr = 0; iconTypeItr < kNumIconTypes; iconTypeItr++)
  122.         {
  123.             handle = iconHandles[iconTypeItr];
  124.             
  125.             if (handle != nil) { DisposeHandle(handle); }
  126.         }
  127.         
  128.         return nil;
  129.     }
  130.  
  131.     // == add the icons to the suite == 
  132.     for(iconTypeItr = 0; iconTypeItr < kNumIconTypes; iconTypeItr++)
  133.     {
  134.         type = iconResourceTypes[iconTypeItr];
  135.         handle = iconHandles[iconTypeItr];
  136.  
  137.         if (handle != nil)
  138.         {
  139.             OSErr harmlessError;    // only error is paramErr which we shouldn't get 
  140.             
  141.             harmlessError = AddIconToSuite(handle, theIconSuite, type);
  142.             WARNING(harmlessError == noErr,    "Resource_Get1IconSuite AddIconToSuite (paramErr?)");
  143.         }
  144.     }
  145.     
  146.     return theIconSuite;
  147. }
  148.  
  149.  void Resource_CleanResHandle(Handle inResourceHandle)
  150. {
  151.     // step 1 detach    
  152.     DetachResource(inResourceHandle);
  153.     RES_WARNING("Resource_CleanResHandle DetachResource failed");
  154.     
  155.     // step 2 clean up the handle 
  156.     HNoPurge(inResourceHandle);
  157.     RES_WARNING("Resource_CleanResHandle HNoPurge failed");
  158.  
  159.     // assert success
  160.     WARNING(*inResourceHandle != nil,    "ResourceToCleanHandle ended up with an empy handle!");
  161. }
  162.  
  163. void Resource_Delete1(ResType inType, short inID, OSStatus *err)
  164. {
  165.     *err = noErr;
  166.  
  167.     // get the resource and error check
  168.     Handle resHandle = Get1Resource(inType, inID);
  169.     *err = ResError();
  170.     if ((*err == noErr) && (resHandle == nil)) { *err = resNotFound; }
  171.     if (*err) { return; }
  172.     
  173.     RemoveResource(resHandle);
  174.     RES_WARNING("Resource_Delete1 RemoveResource failed");
  175.  
  176.     DisposeHandle(resHandle);
  177.     RES_WARNING("Resource_Delete1 DisposeHandle failed");
  178. }
  179.  
  180.  
  181. void Resource_GetString(short inResourceId, SInt16 inDestSize, StringPtr outString, OSStatus *error)
  182. {
  183.     WARNING(outString != nil,        "Resource_GetString outString was nil");
  184.     WARNING(error != nil,            "Resource_GetString error was nil");
  185.     WARNING(inDestSize > 0,            "Resource_GetString inDestSize was <= 0");
  186.     
  187.     *error = noErr;                        // assume no error
  188.  
  189.     StringHandle aStringH;                // the string handle we loaded from a resource
  190.  
  191.     aStringH = GetString(inResourceId);
  192.     WARNING(aStringH != nil,            "Resource_GetString failed!");
  193.     
  194.     if (aStringH == nil)
  195.     {
  196.         outString[0] = 0;
  197.         *error = memFullErr;
  198.         return;
  199.     }
  200.     
  201.     // copy the string
  202.     CopyPStr(*aStringH, outString, inDestSize);
  203.     
  204.     // release the resource
  205.     ReleaseResource( (Handle) aStringH);
  206. }
  207.  
  208.  
  209. void Resource_Copy(short inFromRef, short inFromID, ResType inType, short inToRef, short inToID, OSStatus *error)
  210. {
  211.     StCurResFileState stackResFileSaver;
  212.     
  213.     Handle theResource;
  214.     
  215.     // use the source ref
  216.     UseResFile(inFromRef);
  217.     *error = ResError();
  218.     if (*error != noErr) { return; }
  219.     
  220.     // get the resource
  221.     theResource = Get1Resource(inType, inFromID);
  222.     *error = ResError();    
  223.     if (theResource == nil) { *error = resNotFound; }
  224.     if (*error != noErr) { return; }
  225.     
  226.     // get the name from the resource info
  227.     Str255 name;
  228.  
  229.     short ignoredID;
  230.     ResType ignoredType;
  231.         
  232.     GetResInfo(theResource, &ignoredID, &ignoredType, name);
  233.  
  234.     // detacht this resource from that file
  235.     Resource_CleanResHandle(theResource);
  236.  
  237.     // use the destinatino resource file        
  238.     UseResFile(inToRef);
  239.     *error = ResError();
  240.     if (*error != noErr)
  241.     {
  242.         DisposeHandle(theResource);
  243.         return;
  244.     }
  245.  
  246.     // add this resource
  247.     AddResource(theResource, inType, inToID, name);
  248.     *error = ResError();
  249.  
  250.     // release this resource
  251.     ReleaseResource(theResource);
  252.     *error = ResError();
  253. }
  254.  
  255.  
  256. // ===========================================================================
  257. //    StCurResFileState
  258. // ===========================================================================
  259.  
  260. StCurResFileState::StCurResFileState(void)
  261. {
  262.     Save();
  263. }
  264.  
  265. StCurResFileState::StCurResFileState(short inNewCurResFile)
  266. {
  267.     Save();
  268.     UseResFile(inNewCurResFile);
  269. }
  270.  
  271. StCurResFileState::~StCurResFileState()
  272. {
  273.     Restore();
  274. }
  275.  
  276.  
  277. void
  278. StCurResFileState::Save(void)
  279. {
  280.     mResFileRefNum = CurResFile();
  281. }
  282.  
  283.  
  284. void
  285. StCurResFileState::Restore()
  286. {
  287.     UseResFile(mResFileRefNum);
  288. }
  289.  
  290.  
  291.